package com.cpj.bsyyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpj.bsyyzx.entity.Permission;
import com.cpj.bsyyzx.entity.RolePermission;
import com.cpj.bsyyzx.mapper.RolePermissionMapper;
import com.cpj.bsyyzx.service.IPermissionService;
import com.cpj.bsyyzx.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-05-09
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Autowired
    IPermissionService permissionService;
    @Override
    public List<Permission> getPermission() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",0);
        wrapper.eq("active",1);
        List<Permission> list = permissionService.list(wrapper);
        for(Permission permission : list) {
            getChildren(permission);
        }
        return list;
    }

    @Override
    public RolePermission getByRoleId(Integer roleId) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        return this.getOne(wrapper);
    }

    @Override
    public void getChildren(Permission Permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",Permission.getPermissionId());
        wrapper.eq("active",1);
        List<Permission> list = permissionService.list(wrapper);
        for(Permission permission : list) {
            getChildren(permission);
        }
        Permission.setChildren(list);
    }
}
